import pymysql


class student_model:
    def __init__(self):
        self.db = pymysql.connect(host='43.135.137.253', user='root', password='qazwsx', database='d2_trh')

    def insert_student(self, id, name, sex, major):
        cursor = self.db.cursor()
        sql = 'insert into student_info (id, name, sex, major) values(%s,%s, %s, %s)'
        cursor.execute(sql, (id, name, sex, major))
        self.db.commit()
        cursor.close()

    def del_student(self, id):
        cursor = self.db.cursor()
        sql = 'delete from student_info where id = %s'
        cursor.execute(sql, (id))
        self.db.commit()
        cursor.close()

    def update_name(self, id, value):
        cursor = self.db.cursor()
        sql = 'update  student_info set name = %s where id = %s'
        cursor.execute(sql, (value, id))
        self.db.commit()
        cursor.close()

    def query(self):
        sql = "SELECT * FROM student_info"
        cursor = self.db.cursor()
        cursor.execute(sql)
        # 获取查询结果
        result = cursor.fetchall()
        for row in result:
            print(row)
